<template>
  <Layout class="p-5 bg-white">
    <Form :label-width="90">
      <FormItem class="w-50" label="推进人">
        <label>{{ reservationTry.autorNickName }}</label>
      </FormItem>

      <FormItem class="w-50" label="学生姓名">
        <label>{{ reservationTry.studentName }}</label>
      </FormItem>

      <FormItem class="w-1 m-2" label="新增预约试听日期">
        <Col span="10" push="1">
          <Date-picker
            :value="this.formatDate(new Date()) + ' 00:00:00'"
            :class="'m-2'"
            type="datetime"
            placeholder="预约到访开始时间"
            @on-change="dateChange"
          ></Date-picker>
        </Col>
      </FormItem>
      <FormItem class="text-right">
        <Button @click="submit" type="primary">提交</Button>
        <Button @click="cancel" class="ml-2">取消</Button>
      </FormItem>
    </Form>
  </Layout>
</template>

<script>
export default {
  data() {
    return {
      reservationTry: {
        autorNickName: '',
        studentName: '',
        autor: null,
        cu: null,
        ct: '',
        uu: null,
        ut: '',
        addReservationTryTime: this.formatDate(new Date()) + ' 00:00:00',
      },
      allStages: [], //XX
    }
  },
  methods: {
    dateChange: function(datetime) {
      this.reservationTry.addReservationTryTime = datetime
    },
    changeP: function(selectedPermissionId) {
      if (selectedPermissionId === 0) {
        this.$data.permission.path = ''
      }
    },
    submit: function() {
      //submit----
      let self = this

      if (this.$data.reservationTry.addReservationTryTime === '') {
        this.message.error('请输入预约试听日期')
        return
      }
      var reservationTry = {
        studentId: this.$route.params.row.studentId,
        datetime: this.reservationTry.addReservationTryTime,
        uu: this.reservationTry.uu,
        id: this.$route.params.row.reservationTryId,
        autor: this.$route.params.row.autorId,
      }

      this.http
        .put({
          url: '/common/try',
          param: reservationTry,
        })
        .then((data) => {
          self.message.info('添加成功')
          if (this.$route.params.stageId === 1) {
            this.$tabs.close({ to: '/resource' })
          }

          if (this.$route.params.stageId === 2) {
            this.$tabs.close({ to: '/interview' })
          }

          if (this.$route.params.stageId === 3) {
            this.$tabs.close({ to: '/try' })
          }
        })
    }, //submit----
    cancel: function() {
      if (this.$route.params.stageId === 1) {
        this.$tabs.close({ to: '/resource' })
      }

      if (this.$route.params.stageId === 2) {
        this.$tabs.close({ to: '/interview' })
      }

      if (this.$route.params.stageId === 3) {
        this.$tabs.close({ to: '/try' })
      }
    },
    init: function() {
      let self = this
    },
    formatDate: function(date) {
      //格式化日期方法!
      var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear()

      if (month.length < 2) month = '0' + month
      if (day.length < 2) day = '0' + day

      return [year, month, day].join('-')
    },
  },
  destroyed: function() {
    //跳转页面!
    if (this.$route.params.stageId === 1) {
      this.$router.push({
        name: 'resource',
      })
    }

    if (this.$route.params.stageId === 2) {
      this.$router.push({
        name: 'interview',
      })
    }

    if (this.$route.params.stageId === 3) {
      this.$router.push({
        name: 'try',
      })
    }
  },
  created: function() {
    this.init()
    var _this = this
    _this.reservationTry.autorNickName = this.$route.params.row.autor
    _this.reservationTry.studentName = this.$route.params.row.name
    let currentUser = JSON.parse(sessionStorage.getItem('CURRENT_USER'))
    this.reservationTry.uu = currentUser.user.userId
  },
}
</script>

<style></style>
